package com.cman777.springc.mybatis.config;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.cman777.springc.mybatis.idgenerator.RedisIdGenerator;
import com.cman777.springc.mybatis.objecthandler.CustomMetaObjectHandler;
import com.cman777.springc.mybatis.properties.MybatisProperties;
import com.cman777.springc.mybatis.sqinjector.CustomMybatisPlusSqlInjector;
import com.cman777.springc.mybatis.typehandler.EntityEnumIFaceHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author chenzhicong
 * @time 2020/3/16 14:47
 * @description
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
@ComponentScan("com.cman777.springc.mybatis")
@Log4j2
public class MybatisConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    /**
     * 自动填充
     */
    @Bean
    public MetaObjectHandler myObjectHandler() {
        return new CustomMetaObjectHandler();
    }

    /**
     * id生成
     */
    @Bean
    public IdentifierGenerator redisIdGenerator() {
        return new RedisIdGenerator();
    }
    /**
      * 自定义sql注入（添加自定义mapper方法）
      */
    @Bean
    public CustomMybatisPlusSqlInjector customMybatisPlusSqlInjector(){
        return new CustomMybatisPlusSqlInjector();
    }





}
